//
//  ToolViewController.m
//  BDD_TWO
//
//  Created by XTW on 2023/11/6.
//

#import "ToolViewController.h"
#import "TabBarView.h"
#import "ToolImageEditViewController.h"
#import "ToolImageScanViewController.h"
#import "ToolVideoEditViewController.h"
#import "RecordViewController.h"
#import "ChooseFileViewController.h"
#import "SGQRCode.h"
#import "QQQRCodeVC.h"
#import "CustomAlertView.h"

@interface ToolViewController () {
    TabBarView                 *tabBarView;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation ToolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"工具箱";
    tabBarView = [TabBarView sharedTabBarView];
    [self.navigationItem setHidesBackButton:YES];
    [self initView];
    
    //创建录音存放目录，防止后期经常创建
    NSString *docsDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"records"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:docsDir]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:docsDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.fd_interactivePopDisabled = YES;
    [self.view addSubview:tabBarView];
    [tabBarView setCurrentViewControllerIndex:1];
    
    if (![UserManger hasUserLogged]) {
        UserInfo *user = [UserManger getUserWithId:@"游客"];
        if (!user) {
            user = [[UserInfo alloc] init];
            user.userName = @"游客";
            user.platformId = @"游客";
            [UserManger setUser:user];
        }
        [DEFAULTS setObject:user.userName forKey:USERID];
        [DEFAULTS synchronize];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self popGestureClose:self];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self popGestureOpen:self];
}

- (IBAction)imageEditPress {
    ToolImageEditViewController *viewCon = [[ToolImageEditViewController alloc] initWithNibName:@"ToolImageEditViewController" bundle:nil];
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)imageScanPress {
    ToolImageScanViewController *viewCon = [[ToolImageScanViewController alloc] initWithNibName:@"ToolImageScanViewController" bundle:nil];
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)videoEditPress {
    ToolVideoEditViewController *viewCon = [[ToolVideoEditViewController alloc] initWithNibName:@"ToolVideoEditViewController" bundle:nil];
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)recordingPress {
    RecordViewController *viewCon = [[RecordViewController alloc] initWithNibName:@"RecordViewController" bundle:nil];
    CustomNavigationCoutroller *navCon = [[CustomNavigationCoutroller alloc] init];
    navCon.viewControllers = @[viewCon];
    navCon.modalPresentationStyle = UIModalPresentationFullScreen;
    [[UIViewControllerCJHelper findCurrentShowingViewController].navigationController presentViewController:navCon animated:YES completion:nil];
}

- (IBAction)recordEditPress {
    ChooseFileViewController *viewCon = [[ChooseFileViewController alloc] initWithNibName:@"ChooseFileViewController" bundle:nil];
    viewCon.type = 1;
    CustomNavigationCoutroller *navCon = [[CustomNavigationCoutroller alloc] init];
    navCon.viewControllers = @[viewCon];
    navCon.modalPresentationStyle = UIModalPresentationFullScreen;
    [self.navigationController presentViewController:navCon animated:YES completion:nil];
}

- (IBAction)recorExportPress {
    ChooseFileViewController *viewCon = [[ChooseFileViewController alloc] initWithNibName:@"ChooseFileViewController" bundle:nil];
    viewCon.type = 3;
    CustomNavigationCoutroller *navCon = [[CustomNavigationCoutroller alloc] init];
    navCon.viewControllers = @[viewCon];
    navCon.modalPresentationStyle = UIModalPresentationFullScreen;
    [self.navigationController presentViewController:navCon animated:YES completion:nil];
}

- (IBAction)scanCodePress {
    [SGPermission permissionWithType:SGPermissionTypeCamera completion:^(SGPermission * _Nonnull permission, SGPermissionStatus status) {
        if (status == SGPermissionStatusNotDetermined) {
            [permission request:^(BOOL granted) {
                if (granted) {
                    NSLog(@"第一次授权成功");
                    UIViewController *VC = [[QQQRCodeVC alloc] init];
                    [self.navigationController pushViewController:VC animated:YES];
                } else {
                    NSLog(@"第一次授权失败");
                }
            }];
        } else if (status == SGPermissionStatusAuthorized) {
            NSLog(@"SGPermissionStatusAuthorized");
            UIViewController *VC = [[QQQRCodeVC alloc] init];
            [self.navigationController pushViewController:VC animated:YES];
        } else if (status == SGPermissionStatusDenied) {
            NSLog(@"SGPermissionStatusDenied");
            [self failed];
        } else if (status == SGPermissionStatusRestricted) {
            NSLog(@"SGPermissionStatusRestricted");
            [self unknown];
        }
    }];
}

- (void)failed {
    CustomAlertView *alertView = [[CustomAlertView alloc] initItem:@"[前往：设置 - 隐私 - 相机 - 赏金侠] 打开访问开关" withButtons:[NSArray arrayWithObjects:@"好的", nil]];
    [alertView show];
}

- (void)unknown {
    CustomAlertView *alertView = [[CustomAlertView alloc] initItem:@"未检测到您的摄像头" withButtons:[NSArray arrayWithObjects:@"好的", nil]];
    [alertView show];
}

- (void)initView {
    [self.scrollView setFrame:CGRectMake(0, IS_iPhoneX_Top + NavBarHeight, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Top - NavBarHeight - TabbarHeight)];
    [self.scrollView setContentSize:CGSizeMake(0, 578)];
}
@end
